package game;

import java.util.LinkedList;
import java.util.Random;

public class CardDeck {
	private LinkedList<Card> cardDeck;
	
	public CardDeck() {
		cardDeck = new LinkedList<Card>();
	}
	
	public void reset() {
		cardDeck.clear();
	}
	
	public void addNewDeck() {
		if(GameSettings.miniDecks)
			addMiniDeck();
		else
			addNormalDeck();
		for(int i=0;i<GameSettings.jkrPerDeck;i++) {
			cardDeck.add(new Card(Suit.UNDEFINED, Rank.UNDEFINED));
		}
		shuffleDeck();
	}
	
	private void addNormalDeck() {
		Suit theSuit = Suit.HEARTS;
		while (true) {
			cardDeck.add(new Card(theSuit, Rank.ACE));
			cardDeck.add(new Card(theSuit, Rank.TWO));
			cardDeck.add(new Card(theSuit, Rank.THREE));
			cardDeck.add(new Card(theSuit, Rank.FOUR));
			cardDeck.add(new Card(theSuit, Rank.FIVE));
			cardDeck.add(new Card(theSuit, Rank.SIX));
			cardDeck.add(new Card(theSuit, Rank.SEVEN));
			cardDeck.add(new Card(theSuit, Rank.EIGHT));
			cardDeck.add(new Card(theSuit, Rank.NINE));
			cardDeck.add(new Card(theSuit, Rank.TEN));
			cardDeck.add(new Card(theSuit, Rank.JACK));
			cardDeck.add(new Card(theSuit, Rank.QUEEN));
			cardDeck.add(new Card(theSuit, Rank.KING));
			if (theSuit == Suit.HEARTS)
				theSuit = Suit.DIAMONDS;
			else if (theSuit == Suit.DIAMONDS)
				theSuit = Suit.CLUBS;
			else if (theSuit == Suit.CLUBS)
				theSuit = Suit.SPADES;
			else
				break;
		}
	}
	
	public void addMiniDeck() {
		Suit theSuit = Suit.HEARTS;
		while (true) {
			cardDeck.add(new Card(theSuit, Rank.ACE));
			cardDeck.add(new Card(theSuit, Rank.NINE));
			cardDeck.add(new Card(theSuit, Rank.TEN));
			cardDeck.add(new Card(theSuit, Rank.JACK));
			cardDeck.add(new Card(theSuit, Rank.QUEEN));
			cardDeck.add(new Card(theSuit, Rank.KING));
			if (theSuit == Suit.HEARTS)
				theSuit = Suit.DIAMONDS;
			else if (theSuit == Suit.DIAMONDS)
				theSuit = Suit.CLUBS;
			else if (theSuit == Suit.CLUBS)
				theSuit = Suit.SPADES;
			else
				break;
		}
	}
	
	public void shuffleDeck() {
		LinkedList<Card> tempDeck = new LinkedList<Card>();
		Random generator = new Random();
		while (!cardDeck.isEmpty()) {
			int index = generator.nextInt(cardDeck.size());
			tempDeck.add(cardDeck.get(index));
			cardDeck.remove(index);
		}
		cardDeck = tempDeck;
	}
	
	public Card getCard() {
		if(cardDeck.isEmpty())
			return null;
		else
			return cardDeck.removeFirst();
	}
}
